package base.thread_JUC2.chapter04.demo04;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

/**
 *
 */
public class TestCase {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ThreadPoolExecutor pool = (ThreadPoolExecutor) Executors.newFixedThreadPool(5);

        // 收集任务
        ArrayList<Task> tasks = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Task task = new Task("Task-"+i);
            tasks.add(task);
        }

        // 执行任务
        List<Future<Result>> futures = pool.invokeAll(tasks);

        pool.shutdown();

        // 任务执行结果
        for (Future<Result> future : futures) {
            Result result = future.get();
            System.out.println(result.getName()+"----"+ result.getValue());
        }

    }
}
